package com.risetechnology.krystalkitchen1;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Configuration;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.risetechnology.common.Shared;
import com.risetechnology.master.NavigationActivity;

public class ActivityTips extends NavigationActivity {

	private TextView textviewOurTips;
	private TextView textviewMyTips;
	private EditText editTextMyTipsContent;
	private LinearLayout layoutEditsAction;

	boolean isKeyboardHide = true;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_NO_TITLE);

		setContentView(R.layout.activity_tips);

		setContents();
	}

	private void setContents() {
//		final View activityRootView = findViewById(R.id.root_view);
//		activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
//		    @Override
//		    public void onGlobalLayout() {
//		        int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight();
//		        if (heightDiff > 100) { 
//		        	isKeyboardHide = false;
//		        	editTextMyTipsContent.setCursorVisible(true);
//		        	Toast.makeText(getBaseContext(), "XXXXXXXXXXX", Toast.LENGTH_SHORT).show();
//		        } else {
//		        	isKeyboardHide = true;
//		        	editTextMyTipsContent.setCursorVisible(false);
//		        	Toast.makeText(getBaseContext(), "AAAAAAAAAa", Toast.LENGTH_SHORT).show();
//		        }
//		     }
//		});
		
		
		TextView textviewTipsAndGeneral = (TextView) findViewById(R.id.tv_tips_general);
		textviewTipsAndGeneral.setTypeface(Shared.font);

		layoutEditsAction = (LinearLayout) findViewById(R.id.ll_edits_action);

		// Our tips
		final TextView textviewOurTipsContent = (TextView) findViewById(R.id.tv_our_tips_contents);
		textviewOurTipsContent.setTypeface(Shared.font);

		// My tips
		editTextMyTipsContent = (EditText) findViewById(R.id.et_my_tips_contents);
		String savedTips = Shared.dbAdapter.getTips();
		editTextMyTipsContent.setText(savedTips);
		editTextMyTipsContent.setTypeface(Shared.font);

		editTextMyTipsContent.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				editTextMyTipsContent.setCursorVisible(true);
				isKeyboardHide = false;
			}
		});

		editTextMyTipsContent.addTextChangedListener(new TextWatcher() {
			public void onTextChanged(CharSequence s, int start, int before, int count) {

			}

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}

			public void afterTextChanged(Editable s) {
				String currentTips = editTextMyTipsContent.getText().toString();
				String myTips = Shared.dbAdapter.getTips();
				if ((myTips == null && currentTips == null)
						|| (myTips == null && currentTips.length() == 0)) {
					layoutEditsAction.setVisibility(View.GONE);
				} else if (currentTips.equals(myTips)) {
					layoutEditsAction.setVisibility(View.GONE);
				} else {
					layoutEditsAction.setVisibility(View.VISIBLE);
				}
			}
		});

		textviewOurTips = (TextView) findViewById(R.id.tv_our_tips);
		textviewOurTips.setTypeface(Shared.font);
		textviewOurTips.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				textviewOurTipsContent.setVisibility(View.VISIBLE);
				editTextMyTipsContent.setVisibility(View.GONE);
				
				textviewOurTips.setBackgroundColor(getResources().getColor(R.color.bg_tab_selected));
				textviewOurTips.setTextColor(getResources().getColor(R.color.white));
				
				textviewMyTips.setBackgroundColor(getResources().getColor(R.color.bg_tab_normal));
				textviewMyTips.setTextColor(getResources().getColor(R.color.gray));

				layoutEditsAction.setVisibility(View.GONE);

				hideKeyboard();
			}
		});

		textviewMyTips = (TextView) findViewById(R.id.tv_my_tips);
		textviewMyTips.setTypeface(Shared.font);
		textviewMyTips.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				textviewOurTipsContent.setVisibility(View.GONE);
				editTextMyTipsContent.setVisibility(View.VISIBLE);
				//layoutEditsAction.setVisibility(View.VISIBLE);

				textviewMyTips.setBackgroundColor(getResources().getColor(R.color.bg_tab_selected));
				textviewMyTips.setTextColor(getResources().getColor(R.color.white));
				
				textviewOurTips.setBackgroundColor(getResources().getColor(R.color.bg_tab_normal));
				textviewOurTips.setTextColor(getResources().getColor(R.color.gray));
			}
		});

		textviewOurTips.setBackgroundColor(getResources().getColor(R.color.bg_tab_selected));

		TextView textviewSave = (TextView) findViewById(R.id.tv_save_edits);
		textviewSave.setTypeface(Shared.font);
		textviewSave.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				layoutEditsAction.setVisibility(View.GONE);
				hideKeyboard();

				editTextMyTipsContent.setCursorVisible(false);

				boolean isAdded = Shared.dbAdapter.addTips(editTextMyTipsContent.getText().toString());
				if (isAdded) {
					Toast.makeText(ActivityTips.this.getParent(), "My Tips have been saved!", Toast.LENGTH_LONG).show();
				} else {
					Toast.makeText(ActivityTips.this.getParent(), "There's error when saving tips!", Toast.LENGTH_LONG).show();
				}
			}
		});
	}
	
	private void hideKeyboard() {
		InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(editTextMyTipsContent.getWindowToken(), 0);
		isKeyboardHide = true;
	}

	public void onConfigurationChanged(Configuration newConfig) {
	    super.onConfigurationChanged(newConfig);


	    // Checks whether a hardware keyboard is available
	    if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO) {
	        Toast.makeText(this, "keyboard visible", Toast.LENGTH_SHORT).show();
	    } else if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_YES) {
	        Toast.makeText(this, "keyboard hidden", Toast.LENGTH_SHORT).show();
	    }
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {

		if (event.getAction() == KeyEvent.ACTION_DOWN) {
			if (keyCode == KeyEvent.KEYCODE_BACK) {
				if (isKeyboardHide == false) {
					hideKeyboard();
				} else {
					String myTips = Shared.dbAdapter.getTips();
					String currentTips = editTextMyTipsContent.getText().toString();
					if (currentTips != null && !currentTips.equals(myTips)) {
						if (currentTips.length() > 0) {
							showSaveEditsDialog();
						} else {
							super.onBackPressed();
						}
					} else {
						super.onBackPressed();
					}
				}
			}
		}
		
		return false;
	}
	
//	public void onBackPressed() {
//		if (isKeyboardHide == false) {
//			hideKeyboard();
//		} else {
//			if (layoutEditsAction.getVisibility() == View.VISIBLE) {
//				showSaveEditsDialog();
//			} else {
//				super.onBackPressed();
//			}
//		}
//
//	}

	@SuppressWarnings("deprecation")
	private void showSaveEditsDialog() {
		AlertDialog dialog = new AlertDialog.Builder(ActivityTips.this.getParent())
		.create();
		dialog.setMessage("Save Edits to My Tips?");

		dialog.setButton("YES", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				Shared.dbAdapter.addTips(editTextMyTipsContent.getText().toString());
				dialog.dismiss();
				layoutEditsAction.setVisibility(View.GONE);
				Toast.makeText(ActivityTips.this.getParent(), "My Tips have been saved!", Toast.LENGTH_LONG).show();
			}
		});

		dialog.setButton2("NO", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				editTextMyTipsContent.setText(Shared.dbAdapter.getTips());
				layoutEditsAction.setVisibility(View.GONE);
				Toast.makeText(ActivityTips.this.getParent(), "My Tips have not been saved!", Toast.LENGTH_LONG).show();
				editTextMyTipsContent.setCursorVisible(false);
			}
		});

		dialog.show();
	}

	//	private void showKeyboard() {
	//		InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
	//		imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
	//	}

}
